package cn.yunlang.zhangZhongFei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.yunlang.zhangZhongFei.entity.Goods;
import cn.yunlang.zhangZhongFei.entity.UserCollect;
import cn.yunlang.zhangZhongFei.entity.UserLike;
import cn.yunlang.zhangZhongFei.mapper.GoodsMapper;
import cn.yunlang.zhangZhongFei.mapper.UserCollectMapper;
import cn.yunlang.zhangZhongFei.mapper.UserLikeMapper;
import cn.yunlang.zhangZhongFei.service.GoodsService;
import cn.yunlang.zhangZhongFei.vo.request.CompanyVo;
import cn.yunlang.zhangZhongFei.vo.response.R;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 公司表 服务实现类
 *
 * @author ·
 * @since 2025-05-26 14:50:46
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

  @Resource private UserLikeMapper userLikeMapper;

  @Resource private UserCollectMapper userCollectMapper;

  @Override
  public R<IPage<Goods>> getPage(
      Page<Goods> page, Long userId, Long publisherId, String companyName) {
    if (page == null || page.getCurrent() < 1 || page.getSize() < 1) {
      return R.error(400, "分页参数不合法");
    }

    // 构建查询条件
    LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(publisherId != null, Goods::getPublisherId, publisherId);
    wrapper.eq(Goods::getIsDelete, 0);
    wrapper.like(StringUtils.hasText(companyName), Goods::getCompanyName, companyName);
    wrapper.orderByDesc(Goods::getCreateTime);

    // 执行分页查询
    IPage<Goods> pageResult = baseMapper.getPage(page, userId, wrapper);

    // 处理每条记录的点赞和收藏状态
    if (pageResult != null && pageResult.getRecords() != null) {
      for (Goods goods : pageResult.getRecords()) {
        // 查询点赞状态
        LambdaQueryWrapper<UserLike> likeWrapper = new LambdaQueryWrapper<>();
        likeWrapper
            .eq(UserLike::getUserId, userId)
            .eq(UserLike::getLikedUserId, goods.getPublisherId());
        Long likeCount = userLikeMapper.selectCount(likeWrapper);
        goods.setIsLike(likeCount > 0);

        // 查询收藏状态
        LambdaQueryWrapper<UserCollect> collectWrapper = new LambdaQueryWrapper<>();
        collectWrapper
            .eq(UserCollect::getUserId, userId)
            .eq(UserCollect::getCompanyId, goods.getId());
        Long collectCount = userCollectMapper.selectCount(collectWrapper);
        goods.setIsCollect(collectCount > 0);
      }
    }

    return R.success(pageResult);
  }

  @Override
  public R<Goods> getOne(Long goodsId) {
    if (goodsId == null) {
      return R.error(400, "ID不能为空");
    }
    Goods goods = this.getById(goodsId);
    if (goods == null) {
      return R.error(404, "未找到该商品");
    }
    return R.success(goods);
  }

  @Override
  public R<String> addNew(CompanyVo companyVo) {
    if (companyVo == null || !StringUtils.hasText(companyVo.getCompanyName())) {
      return R.error(400, "公司名称不能为空");
    }
    // 名称查重
    LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(Goods::getGoodsName, companyVo.getCompanyName());
    if (this.count(wrapper) > 0) {
      return R.error(400, "公司名称已存在");
    }
    Goods goods = new Goods();
    BeanUtils.copyProperties(companyVo, goods);
    goods.setIsHot("0");
    goods.setLikeCount(0);
    goods.setViewCount(0);
    boolean save = save(goods);
    return save ? R.success("新增成功") : R.error(500, "新增失败");
  }

  @Override
  public R<String> deleteGoods(Long goodsId) {
    if (goodsId == null) {
      return R.error(400, "ID不能为空");
    }
    boolean remove = this.removeById(goodsId);
    return remove ? R.success("删除成功") : R.error(500, "删除失败");
  }

  @Override
  public R<String> updateGoods(Goods goods, String userId) {
    if (goods == null || goods.getId() == null || !StringUtils.hasText(goods.getGoodsName())) {
      return R.error(400, "ID和商品名称不能为空");
    }
    if (!goods.getPublisherId().equals(userId)) {
      return R.error(500, "不是创建者无法修改");
    }
    // 名称查重
    LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(Goods::getGoodsName, goods.getGoodsName()).ne(Goods::getId, goods.getId());
    if (this.count(wrapper) > 0) {
      return R.error(400, "商品名称已存在");
    }
    boolean update = this.updateById(goods);
    return update ? R.success("修改成功") : R.error(500, "修改失败");
  }
}
